package co.fitstart.admin.web.controller.trade.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.admin.web.interceptor.MenuSetting;

import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.entity.product.Product;
import co.fitstart.entity.product.ProductType;
import co.fitstart.admin.service.ProductService;


/**
 * 
 * @author lidan
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/products")
@MenuSetting("menu-ecommerce")
public class ProductController {
    
    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
    
    @Autowired
    private ProductService productService;

    /**
     * List all products by page and type
     * 
     * @param type
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-product-management")
    public ModelAndView list(@RequestParam(value = "type", required = false, defaultValue = "offline") String type,
                             HttpServletRequest request,
                             Page page){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-list");
        
        ProductType productType = ProductType.aliasOf(type);
        
        List<Product> products = productService.list(productType, page);

        modelAndView.addObject("type", productType);
        modelAndView.addObject("products", products);
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * Display status panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/status", method = RequestMethod.GET)
    public ModelAndView editStatus(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-status");
        
        modelAndView.addObject("productId", productId);
        
        return modelAndView;
    }
    
    /**
     * Do edit status
     * 
     * @param product
     * @return
     */
    @RequestMapping(value = "/{productId}/status", method = RequestMethod.PATCH)
    public @ResponseBody String onEditStatus(@Preloading("productId") Product product){
        
        Result result = new Result();
        
        try {
            
            productService.update(product.editStatus());
            
            result.success().message("修改产品状态成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditStatus - ", e);
            
            result.fail().message("修改产品状态失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    @RequestMapping(value = "/{productId}/deleted/", method = RequestMethod.GET)
    public @ResponseBody String onDeletedProduct(@PathVariable("productId") Long productId){
        
        Result result = new Result();
        boolean deleted = true;
        
        try {
            
            productService.deletedProduct(deleted,productId);
            
            result.success().message("删除产品成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditStatus - ", e);
            
            result.fail().message("删除产品失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    
    /**
     * Display hide product panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/display", method = RequestMethod.GET)
    public ModelAndView editDisplay(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-display");
        
        modelAndView.addObject("productId", productId);
        
        return modelAndView;
    }
    
    /**
     * Do hide product
     * 
     * @param product
     * @return
     */
    @RequestMapping(value = "/{productId}/display", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDisplay(@Preloading("productId") Product product){
        
        Result result = new Result();
        
        try {
            
            productService.update(product.editDisplay());
            
            result.success().message("修改产品显示状态成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditDisplay - ", e);
            
            result.fail().message("修改产品显示状态失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display buyable edit panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/buyable", method = RequestMethod.GET)
    public ModelAndView editBuyable(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-buyable");
        
        modelAndView.addObject("productId", productId);
        
        return modelAndView;
    }
    
    /**
     * Do buy-close
     * 
     * @param product
     * @return
     */
    @RequestMapping(value = "/{productId}/buyable", method = RequestMethod.PATCH)
    public @ResponseBody String onEditBuyable(@Preloading("productId") Product product){
        
        Result result = new Result();
        
        try {
            
            productService.update(product.editBuyable());
            
            result.success().message("修改产品购买状态成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditBuyable - ", e);
            
            result.fail().message("修改产品购买状态失败！");
            
        }

        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display stick article panel
     * 
     * @param productId
     * @param type
     * @return
     */
    @RequestMapping(value="/{productId}/stick", method=RequestMethod.GET)
    public ModelAndView stick(@PathVariable("productId") Long productId, @RequestParam(value = "type", required = false, defaultValue = "online") String type){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-stick");
        
        modelAndView.addObject("productId", productId);
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }

    /**
     * Do stick article
     * 
     * @param productId
     * @param type
     * @return
     */
    @RequestMapping(value="/{productId}/stick", method=RequestMethod.PATCH)
    public @ResponseBody String onStick(@PathVariable("productId") Long productId, @RequestParam(value = "type", required = false, defaultValue = "online") String type){
        
        Result result = new Result();
        
        try {
            
            Product product = productService.load(Product.class, productId);
            
            ProductType productType = ProductType.aliasOf(type);
            
            productService.stick(product, productType);
            
            result.success().message("置顶产品成功");
           
        } catch (Exception e) {
           
            logger.error("ProductController#onStick() error:", e);
           
            result.fail().message("置顶产品失败!");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display unstick article panel
     * 
     * @param productId
     * @param type
     * @return
     */
    @RequestMapping(value="/{productId}/unstick", method=RequestMethod.GET)
    public ModelAndView unstick(@PathVariable("productId") Long productId, @RequestParam(value = "type", required = false, defaultValue = "online") String type){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-unstick");
        
        modelAndView.addObject("productId", productId);
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }
    
    /**
     * Do unstick article
     * 
     * @param productId
     * @param type
     * @return
     */
    @RequestMapping(value="/{productId}/unstick", method=RequestMethod.PATCH)
    public @ResponseBody String onUnstick(@PathVariable("productId") Long productId, @RequestParam(value = "type", required = false, defaultValue = "online") String type){
        
        Result result = new Result();
        
        try {
            
            Product product = productService.load(Product.class, productId);
            ProductType productType = ProductType.aliasOf(type);
            
            productService.unStick(product, productType);
            
            result.success().message("取消置顶产品成功");
           
        } catch (Exception e) {
           
            logger.error("ProductController#onUnstick() error:", e);
           
            result.fail().message("取消置顶产品失败!");
        }
        
        return JsonUtils.toJsonString(result);
    }

}
